﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Globalization;
using System.Diagnostics;
using Microsoft.Office.Server.Administration;
using SharePointSiteConfigurator;

namespace SharePointSiteConfigurator
{
#if SP2010
    public sealed class TraceLogger2010 : SPDiagnosticsServiceBase
    {
#region properties

        private const string LogServiceName = "SiteConfigurator";
        private static TraceLogger2010 _Instance = null;
        private static readonly object _Lock = new object();
        SPDiagnosticsCategory cat = new SPDiagnosticsCategory(LogServiceName, TraceSeverity.Verbose, EventSeverity.Information);
        SPDiagnosticsCategory catHigh = new SPDiagnosticsCategory(LogServiceName, TraceSeverity.High, EventSeverity.Error);
#endregion
        public TraceLogger2010() { }
        public TraceLogger2010(string name, SPFarm parent)
            : base(name, parent)
        {
        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>();
            List<SPDiagnosticsCategory> categories = new List<SPDiagnosticsCategory>();

            categories.Add(cat);
            categories.Add(catHigh);

            SPDiagnosticsArea area = new SPDiagnosticsArea(LogServiceName, categories);
            areas.Add(area);

            return areas;

        }
        //<summary>
         //Use this instance to enable logging 
         //</summary>
        public static TraceLogger2010 Instance
        {
            get
            {
                lock (_Lock)
                {
                    if (_Instance == null)
                        _Instance = new TraceLogger2010(LogServiceName, SPFarm.Local);

                    return _Instance;
                }
            }
        }

        #region Public Trace Methods

        /// <summary>
        /// Logs exceptions to a  logfile in \14\Logs
        /// </summary>
        /// <param name="message">Custom string message to write with the error</param>
        /// <param name="ex">Exception message</param>
        public void TraceError(string message, Exception ex)
        {
            this.WriteTrace(0, cat, TraceSeverity.High, string.Format("Message: {0} Error: {1}", message, ex));
        }
        /// <summary>
        /// Logs exceptions to a  logfile in \14\Logs
        /// </summary>
        /// <param name="message">Custom string message to write with the error</param>
        /// <param name="ex">Exception message</param>
        /// <param name="categoryName">The categoryname displayed in the ULS logs</param>
        public void TraceError(string message, Exception ex, string categoryName)
        {
            SPDiagnosticsCategory lcat = new SPDiagnosticsCategory(categoryName, TraceSeverity.Verbose, EventSeverity.Error);
            if (ex != null) this.WriteTrace(0, lcat, TraceSeverity.High, string.Format("Message: {0} Error: {1}", message, ex));
        }
        /// <summary>
        /// Logs exceptions to a  logfile in \14\Logs
        /// </summary>
        /// <param name="message">Custom string message to write with the error</param>
        /// <param name="ex">Exception message</param>
        /// <param name="categoryName">The categoryname displayed in the ULS logs</param>
        public void TraceError(TraceSeverity severity, string message, Exception ex)
        {
            this.WriteTrace(0, cat, severity, string.Format("Message: {0} Error: {1}", message, ex));
        }

        /// <summary>
        /// Logs warnings to a  logfile in \14\Logs
        /// </summary>
        /// <param name="message">Custom string message to write with the warning</param>
        public void TraceWarning(string message)
        {
            this.WriteTrace(0, cat, TraceSeverity.Medium, message);
        }

        /// <summary>
        /// Logs warnings to a  logfile in \14\Logs
        /// </summary>
        /// <param name="message">Custom string message to write with the warning</param>
        /// <param name="ex">Exception message</param>

        public void TraceWarning(string message, Exception ex)
        {
            this.WriteTrace(0, cat, TraceSeverity.Medium, message + ": " + ex.ToString());
        }

        /// <summary>
        /// Logs warnings to a  logfile in \14\Logs
        /// </summary>
        /// <param name="message">Custom string message to write with the warning</param>
        /// <param name="ex">Exception message</param>
        /// <param name="categoryName">The category to trace within.</param>

        public void TraceWarning(string message, Exception ex, string categoryName)
        {
            SPDiagnosticsCategory lcat = new SPDiagnosticsCategory(categoryName, TraceSeverity.Verbose, EventSeverity.Error);
            this.WriteTrace(0, lcat, TraceSeverity.Medium, message + ": " + ex.ToString());
        }

        /// <summary>
        /// Logs warnings to a  logfile in \14\Logs
        /// </summary>
        /// <param name="message">Custom string message to write with the warning</param>
        /// <param name="categoryName">The category to trace within.</param>
        public void TraceWarning(string message, string categoryName)
        {
            SPDiagnosticsCategory lcat = new SPDiagnosticsCategory(categoryName, TraceSeverity.Verbose, EventSeverity.Error);
            this.WriteTrace(0, lcat, TraceSeverity.Medium, message);
        }

        /// <summary>
        /// Logs verbose messages to the  logfile in \14\logs
        /// </summary>
        /// <param name="message">Custom string message to write</param>
        public void TraceVerboseMessage(string message)
        {
            this.WriteTrace(0, cat, TraceSeverity.Verbose, message);
        }
        /// <summary>
        /// Logs verbose messages to the  logfile in \12\logs
        /// </summary>
        /// <param name="message">Custom string message to write</param>

        public void TraceVerboseMessage(string message, string categoryName)
        {
            SPDiagnosticsCategory lcat = new SPDiagnosticsCategory(categoryName, TraceSeverity.Verbose, EventSeverity.Verbose);
            this.WriteTrace(0, lcat, TraceSeverity.Verbose, message);
        }
        /// <summary>
        /// Logs verbose messages to the  logfile in \12\logs
        /// </summary>
        /// <param name="message">Custom string message to write</param>

        public void TraceVerboseMessage(TraceSeverity severity, string message, Exception ex)
        {
            this.WriteTrace(0, cat, severity, string.Format("Message: {0} Error: {1}", message, ex));
        }
        #endregion
    }
#endif 
}
